package com.tuo51.config;

import com.tuo51.components.intercepter.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebInterceptorConfiguration implements WebMvcConfigurer {

    @Bean
    ActionLogInterceptor actionLogInterceptor() {
        return new ActionLogInterceptor();
    }


    @Bean
    AuthInterceptor authInterceptor(){
        return new AuthInterceptor();
    }

    @Bean
    OPAuthInterceptor opAuthInterceptor(){
        return new OPAuthInterceptor();
    }

    @Bean
    MpAuthInterceptor mpAuthInterceptor(){
        return new MpAuthInterceptor();
    }

    @Autowired
    PartnerInterceptor partnerInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(actionLogInterceptor())
            .addPathPatterns("/**")
            .excludePathPatterns("/error/**");

        registry.addInterceptor(authInterceptor())
                .addPathPatterns("/eapi/**")
                .addPathPatterns("/oapi/**")  //观测点和小程序，公用一个拦截器
                .excludePathPatterns("/eapi/manager/detail")
                .excludePathPatterns("/eapi/manager/compare_with")
                .excludePathPatterns("/eapi/bmi/info")
                .excludePathPatterns("/eapi/pay/wx/order/notify")
                .excludePathPatterns("/eapi/news/article/**")
                .excludePathPatterns("/eapi/news/wordArticleTest");

        registry.addInterceptor(mpAuthInterceptor())
                .addPathPatterns("/papi/**")
                .excludePathPatterns("/papi/pb/detail") ;


        registry.addInterceptor(opAuthInterceptor())
                .addPathPatterns("/mview/")
                .addPathPatterns("/mview/**")
                .addPathPatterns("/mapi/**")
                .excludePathPatterns("/mapi/auth/**")
                .excludePathPatterns("/mview/login") ;

        registry.addInterceptor(partnerInterceptor)
                .addPathPatterns("/partner/")
                .addPathPatterns("/partner/**")
                .excludePathPatterns("/partner/org/report_count")
                .excludePathPatterns("/partner/org/children_count");
    }

    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
            "classpath:/META-INF/resources/",
            "classpath:/resources/",
            "classpath:/static/",
            "classpath:/public/",
            "classpath*:/META-INF/resources/",
    };

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);
    }

}
